package fu.abc.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




import com.sun.xml.internal.ws.api.message.Messages;

import fu.abc.DAO.AccountDAO;

/**
 * Servlet implementation class LoginAction
 */
@WebServlet("/LoginAction")
public class LoginAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html;charset=UTF-8");
		
		String username = request.getParameter("txtaccount").toString().trim();
		String password = request.getParameter("txtpassword").toString().trim();
		
		String message = "Tên đăng nhập hoặc mật khẩu sai";
		AccountDAO manage = new AccountDAO();
		
		try {
			String role = manage.login(username, password);
			if(role!=null){
				if(role.equals("System Admin")){
						HttpSession session = request.getSession();
						session.setAttribute("acc", username);
						response.sendRedirect("/Vnplace/systemAdmin/systemAdmin.jsp");
						
				}else{
						HttpSession session = request.getSession();
						session.setAttribute("acc", username);
						response.sendRedirect("/Vnplace/content-editor/contenteditor.jsp");
				}
			}else{
				request.setAttribute("errormessage", message);
				RequestDispatcher dis = getServletContext().getRequestDispatcher("/systemAdmin/login.jsp");
				dis.forward(request, response);
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
